/**
 * Created by dell on 16-8-12.
 */
$(document).ready(function(){
    addjumpPagesEvent();
    showSumCount();
    goodsInfo=getGoodsInfo();
    showCartList(goodsInfo);
    promotionInfo=getPromotionInfo(goodsInfo);
    promotionSumPrice=computePromotionSumPrice(promotionInfo);
    showPromotionInfo(promotionInfo,promotionSumPrice);
    sumPrice=computeSumPrice(goodsInfo);
    showSumPrice(sumPrice);
    showDate();
    addConfirmEvent();
})
function addjumpPagesEvent(){
    $("#label").click(function () {
        location.assign("../html/productList.html");
    });
    $("#home").click(function () {
        location.assign("../html/home.html");
    });
    $("#productList").click(function () {
        location.assign("../html/productList.html");
    })
}
function dateDigitToString(num) {
    return num < 10 ? '0' + num : num;
};
function  showDate(){
    var currentDate = new Date();
        year = dateDigitToString(currentDate.getFullYear());
        month = dateDigitToString(currentDate.getMonth() + 1);
        date = dateDigitToString(currentDate.getDate());
        hour = dateDigitToString(currentDate.getHours());
        minute = dateDigitToString(currentDate.getMinutes());
        second = dateDigitToString(currentDate.getSeconds());
        dateString = year + '年' + month + '月' + date + '日 ' + hour + ':' + minute + ':' + second;
    $("#time").html(dateString);
}
function showSumCount(){
    var cartCount=JSON.parse(localStorage.getItem("cartCount"))||0;
        $("#cartCount").html(cartCount);
}
function getGoodsInfo(){
    return JSON.parse(localStorage.getItem("goodsInfo"));
}
function showCartList(goodsInfo){
    $("#templet").tmpl(goodsInfo).appendTo('#cartInfo');
}
function getPromotionInfo(goodsInfo){
    var promotionInfo=[];
    _.each(goodsInfo,function(item){
        if(item.promotionCount>=1){
            promotionInfo.push(item);
        }
    });
    return  promotionInfo;
}
function showPromotionInfo(promotionInfo,promotionSumPrice){
        if(promotionSumPrice==0){
            $("#promotionInfo").html("");
        }else{
            showPromotionList(promotionInfo);
            showPromotionSumPrice(promotionSumPrice);
        }
}
function showPromotionList(promotionInfo){
    $("#promotionTemplet").tmpl(promotionInfo).appendTo('#promotionList');
}
function computeSumPrice(goodsInfo){
    var sumPrice=0;
    _.each(goodsInfo,function(item){
        sumPrice+=item.count*item.price;
    });
    return sumPrice.toFixed(2);
}
function showSumPrice(sumPrice){
    $("#sumPrice").html("总计："+sumPrice+"元");
}
function computePromotionSumPrice(promotionInfo){
    var promotionSumPrice=0;
    _.each(promotionInfo,function(item){
            promotionSumPrice += item.promotionCount * item.price;
        });
    return promotionSumPrice.toFixed(2);
}
function showPromotionSumPrice(promotionSumPrice){
    $("#promotionSumPrice").html("节省："+promotionSumPrice+"元");
}
function  addConfirmEvent(){
    $(".confirm").click(function(){
        location.assign("../html/productList.html");
        localStorage.clear();
    })
}
